<?php
namespace Home\Model;
use  Think\Model;
class CategoryModel extends Model{

    protected $autoCheckFields =false;
    public function count_goods($where='')
    {
        $DB_PREFIX=C('DB_PREFIX');
      
        // 取出所有符合条件的商品数据，并将结果存入对应的推荐类型数组中
        $sql = 'SELECT count(*) as num ' 
            . 'FROM ' . $DB_PREFIX . 'goods as g ' ;
        $sql .= ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0  ' . $where;

        $result = M()->query($sql);


        return $result[0]['num'];
    }
    //最低价
    public function get_lowest_price($where='')
    {
        $DB_PREFIX=C('DB_PREFIX');
      
        // 取出所有符合条件的商品数据，并将结果存入对应的推荐类型数组中
        $sql = 'SELECT shop_price as lowest_price ' 
            . 'FROM ' . $DB_PREFIX . 'goods as g ' ;
        $sql .= ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0  ' . $where." order by shop_price asc limit 1";

        $result = M()->query($sql);


        return $result[0]['lowest_price'];
    }
 /**
     * 获取推荐商品
     * @param  $type
     * @param  $limit
     * @param  $start
     */
    public function goods_list($session=array(),$where='', $limit = 10, $start = 0,$order=' ') {
        //配置文件
        $config=M('shop_config')->getField('code,value');
        if($config['sort_order_method']==1)
        {$desc=' asc';}
        else
        {$desc=' desc';}

        if($config['sort_order_type']==1)
        {
            $mr_order=' g.shop_price '.$desc;
        }
        else if($config['sort_order_type']==2)
        {
             $mr_order=' g.last_update '.$desc;
        }
        else
        { $mr_order=' g.add_time '.$desc;}
        


        $order=$order?$order.",g.sort_order desc":$mr_order;

        $no_pic='data/common/images/no_picture.gif';//没有产品时

    	$DB_PREFIX=C('DB_PREFIX');
        // 取出所有符合条件的商品数据，并将结果存入对应的推荐类型数组中
        $sql = 'SELECT g.sales_volume_base,g.goods_id, g.goods_name, g.click_count, g.goods_name_style, g.market_price,'
            .' g.shop_price AS org_price, g.promote_price, ' . "IFNULL(mp.user_price, g.shop_price * "
            ."'$session[discount]') AS shop_price, " . "promote_start_date, promote_end_date,"
            ." g.goods_brief, g.goods_thumb, g.goods_img, xl.sales_volume " 
            . 'FROM ' . $DB_PREFIX . 'goods AS g ' 
             .' LEFT JOIN ecs_touch_goods AS xl ON g.goods_id=xl.goods_id '
            . "LEFT JOIN " . $DB_PREFIX . "member_price AS mp " 
            . "ON mp.goods_id = g.goods_id AND mp.user_rank = '$session[user_rank]' ";
        $sql .= ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0  ' . $where;
        $sql .= ' ORDER BY '.$order.'  limit ' . $start . ', ' . $limit;

        $result = M()->query($sql);

        // print_r($sql);exit();

        foreach ($result as $key => $vo) {
            

            if ($vo['promote_price'] > 0) {

                $promote_price = bargain_price($vo['promote_price'], $vo['promote_start_date'], $vo['promote_end_date']);

                 $goods[$key]['promote_price']= $promote_price > 0 ? price_format($promote_price): '';

            } else {
                $goods[$key]['promote_price'] = '';
            }
             //计算属性的最低价格之和
             // $msum_properties_price = D('Goods')->msum_properties_price($vo['goods_id']);


             // if($msum_properties_price)
             // {
             //     $vo['market_price']=$vo['market_price']+floatval($msum_properties_price);
             //     $vo['shop_price']=$vo['shop_price']+floatval($msum_properties_price);
             //     if ($vo['promote_price'] > 0)
             //     $goods[$key]['promote_price']=price_format($promote_price+floatval($msum_properties_price));
                  
             // }
              

             

            $goods[$key]['id'] = $vo['goods_id'];
            // $goods[$key]['goods_id'] = $vo['goods_id'];
            $goods[$key]['name'] = $vo['goods_name'];
            // $goods[$key]['brief'] = $vo['goods_brief'];

            // $goods[$key]['short_name'] = sub_str($vo['goods_name'], 7);
            $goods[$key]['market_price'] = price_format($vo['market_price']);
            $goods[$key]['shop_price'] = price_format($vo['shop_price']);
            $goods[$key]['goods_thumb'] = $vo['goods_thumb']?(get_path_url().$vo['goods_thumb']):(get_path_url().$no_pic);
            $goods[$key]['goods_img'] = $vo['goods_img']?(get_path_url().$vo['goods_img']):(get_path_url().$no_pic);
            $goods[$key]['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('goods/index', array('id' => $vo['goods_id']));
            // $goods[$key]['click_count'] = $vo['click_count'];//点击量
            // $goods[$key]['sc'] = D('GoodsBase')->get_goods_collect($vo['goods_id']);//收藏人数
            // $goods[$key]['mysc'] = 0;//用户是否收藏
            // 检查是否已经存在于用户的收藏夹 collect_goods

            //所有的促销活动信息
            // $goods[$key]['promotion'] = model('GoodsBase')->get_promotion_show($vo['goods_id']);


             $vo['sales_volume'] = $vo['sales_volume']?$vo['sales_volume']:0;//销量
             if(empty($vo['sales_volume']))//销量统计增加
             {
                // $nCount=0;
                $nCount =M("touch_goods")->where('goods_id = "' . $vo['goods_id'] . '"')->count();

                if($nCount<1)
                {
                   M()->execute("INSERT INTO " . $DB_PREFIX . "touch_goods (`goods_id` ,`sales_volume` ) VALUES ( '" . $vo['goods_id'] . "' , '0')");
                }
                 
             }
             $goods[$key]['sales_volume'] = $vo['sales_volume_base']+$vo['sales_volume'];
             //订单中获取销量
             //get_goods_count($goods_id);




             // if ($type == 'zk') {
             //    if($goods[$key]['promotion']==null)
             //        {
             //          unset($goods[$key]);
             //        }
             // }
             // else
                  $type_goods[] = $goods[$key];
          
        }
    /*  exit;*/
   /*  var_dump($type_goods[$type]);*/
        return $type_goods;
    }

 /**
     * 获得分类的信息
     *
     * @param integer $cat_id 
     *
     * @return void
     */
  public   function get_cat_rate($cat_id) {

        $where=array();
        $where['cat_id']=$cat_id;
        $row=M('category')->field("parent_id,profit_rate")->where($where)->find();


        if($row['profit_rate']>0)
        {
            return $row['profit_rate'];
        }
        elseif($row['parent_id'])
        {
          $this->get_cat_rate($row['parent_id']);
        }
        return 0;
    }



}
?>
